﻿#include "wqMoveItem.h"
#include "wqBtnItem.h"
#include "wqToolItem.h"
#include "wqDragItem.h"
#include <QGraphicsSceneMouseEvent>

wqMoveItem::wqMoveItem(const QString& itemName, QGraphicsItem* parent)
    : QGraphicsObject(parent)
{
    setFlags(QGraphicsItem::ItemIsSelectable |
             QGraphicsItem::ItemSendsGeometryChanges |
             QGraphicsItem::ItemIsFocusable |
             QGraphicsItem::ItemIsMovable);

    setOpacity(0.8);
	setZValue(0);
	setAcceptHoverEvents(true);

    setData(0,itemName);
    setData(1, true);
	setData(999, 1);

	prepareGeometryChange();
	mBoundingRect = QRectF(0,0,200,100);
	//QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
	//effect->setColor(QColor(63, 63, 63, 180));	
	//effect->setOffset(8,8);

	//setGraphicsEffect(effect);

	btn_top = new wqBtnItem(itemName+"01", this);
	btn_left = new wqBtnItem(itemName+"02", this);
	btn_right = new wqBtnItem(itemName+"03", this);

	QRectF curRect = boundingRect().adjusted(0,10,0,-10);

	btn_top->setPos(QPointF(curRect.center().x()-10, curRect.top()-10));
	btn_left->setPos(QPointF(curRect.center().x()-curRect.width()/3-10, curRect.height()));
	btn_right->setPos(QPointF(curRect.center().x()+curRect.width()/3-10, curRect.height()));

	// Tools
	tool1 = new wqToolItem(this);
	tool2 = new wqToolItem(this);

	tool1->setVisible(false);
	tool2->setVisible(false);

	tool1->setPos(QPointF(curRect.center().x()+curRect.width()/3-40, curRect.height()-20));
	tool2->setPos(QPointF(curRect.center().x()+curRect.width()/3-10, curRect.height()-20));

	// Drags
	wqDragItem* drag1 = new wqDragItem(this);
	wqDragItem* drag2 = new wqDragItem(this);
	wqDragItem* drag3 = new wqDragItem(this);
	wqDragItem* drag4 = new wqDragItem(this);

	drag1->setPos(curRect.topLeft());
	drag2->setPos(curRect.topRight());
	drag3->setPos(curRect.bottomLeft());
	drag4->setPos(curRect.bottomRight());

	connect(drag1, SIGNAL(sig_childMoved()), this, SLOT(slot_changeRect()));
	connect(drag2, SIGNAL(sig_childMoved()), this, SLOT(slot_changeRect()));
	connect(drag3, SIGNAL(sig_childMoved()), this, SLOT(slot_changeRect()));
	connect(drag4, SIGNAL(sig_childMoved()), this, SLOT(slot_changeRect()));

	//setFiltersChildEvents(true);
}

QRectF wqMoveItem::boundingRect() const
{
    return mBoundingRect;
}

void wqMoveItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* item, QWidget* wid)
{
	painter->save();

	QRectF curRect = boundingRect().adjusted(0,10,0,-10);

    if(isSelected())
    {
        painter->setPen(QPen(Qt::yellow,3));
        painter->setBrush(QBrush(GetGreen_linearGradient()));
    }
    else
    {
        painter->setPen(QPen(Qt::blue,3));
        painter->setBrush(QBrush(GetYellow_linearGradient()));
    }

    painter->drawRoundedRect(curRect, 25, 25);

	painter->setPen(QPen(Qt::red, 1, Qt::DotLine));
	painter->setBrush(Qt::NoBrush);
	painter->drawRect(boundingRect());

	painter->drawText(boundingRect().center(),data(0).toString());

	painter->restore();
}

QLinearGradient wqMoveItem::GetGreen_linearGradient(int OFFSET_Y)
{
    // 绿色
    QLinearGradient linear( QPointF(0, 0+OFFSET_Y), QPointF(0, boundingRect().height()+OFFSET_Y));
    linear.setColorAt(0.3, QColor(103,247,91));
    linear.setColorAt(0.6, QColor(97,237,56));
    linear.setColorAt(0.9, QColor(43,43,43));

    return linear;
}

QLinearGradient wqMoveItem::GetYellow_linearGradient(int OFFSET_Y)
{
    // 黄色
    QLinearGradient linear( QPointF(0, 0+OFFSET_Y), QPointF(0, boundingRect().height()+OFFSET_Y));
    linear.setColorAt(0.3, QColor(235,245,105));
    linear.setColorAt(0.6, QColor(223,240,71));
    linear.setColorAt(0.9, QColor(54,58,3));

    return linear;
}

void wqMoveItem::mousePressEvent ( QGraphicsSceneMouseEvent * ev )
{
    if(ev->modifiers() == Qt::ControlModifier)
    {
        setFlags(QGraphicsItem::ItemIsSelectable |
                 QGraphicsItem::ItemSendsGeometryChanges);

        setData(1, false);
    }

    QGraphicsItem::mousePressEvent(ev);
}

void wqMoveItem::mouseMoveEvent(QGraphicsSceneMouseEvent * ev)
{
    QGraphicsItem::mouseMoveEvent(ev);
}

void wqMoveItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * ev)
{
    setFlags(QGraphicsItem::ItemIsSelectable |
             QGraphicsItem::ItemSendsGeometryChanges |
             QGraphicsItem::ItemIsFocusable |
             QGraphicsItem::ItemIsMovable);

    setData(1, true);

    QGraphicsItem::mouseReleaseEvent(ev);
}

QVariant wqMoveItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if(change == ItemPositionChange && scene())
    {
        emit sig_posChanged();
    }

	if(change == ItemPositionHasChanged && scene())
	{
		emit sig_posChanged();
	}

    return QGraphicsItem::itemChange(change, value);
}

void wqMoveItem::contextMenuEvent( QGraphicsSceneContextMenuEvent *event )
{

}

void wqMoveItem::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
{
	QList<QGraphicsItem*> allItems = childItems();
	foreach(QGraphicsItem* item, allItems)
	{
		wqToolItem* tool = dynamic_cast<wqToolItem*>(item);
		if(tool)
		{
			tool->setVisible(true);
		}
	}

	QGraphicsItem::hoverEnterEvent(event);
}

void wqMoveItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
{
	QList<QGraphicsItem*> allItems = childItems();
	foreach(QGraphicsItem* item, allItems)
	{
		wqToolItem* tool = dynamic_cast<wqToolItem*>(item);
		if(tool)
		{
			tool->setVisible(false);
		}
	}

	QGraphicsItem::hoverLeaveEvent(event);
}

void wqMoveItem::slot_changeRect()
{
	QRectF rect(0,0,0,0);
	QList<QGraphicsItem*> allItems = childItems();
	foreach(QGraphicsItem* item, allItems)
	{
		wqDragItem* drag = dynamic_cast<wqDragItem*>(item);
		if(drag)
		{
			QRectF r = drag->mapRectToScene(drag->boundingRect());
			rect = rect.united(r);
		}
	}

	prepareGeometryChange();
	mBoundingRect = mapRectFromScene(rect);

	QRectF curRect = boundingRect().adjusted(0,10,0,-10);

	btn_top->setPos(QPointF(curRect.center().x()-10, curRect.top()-10));
	btn_left->setPos(QPointF(curRect.center().x()-curRect.width()/3-10, curRect.height()+boundingRect().y()));
	btn_right->setPos(QPointF(curRect.center().x()+curRect.width()/3-10, curRect.height()+boundingRect().y()));

	tool1->setPos(QPointF(curRect.center().x()+curRect.width()/3-40, curRect.height()+boundingRect().y()-20));
	tool2->setPos(QPointF(curRect.center().x()+curRect.width()/3-10, curRect.height()+boundingRect().y()-20));

	emit sig_posChanged();
}
